/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import ts from '../../vendor/ohos-typescript';

export function isSupportedLiteral(node: ts.Node): boolean {
  return (
    ts.isStringLiteral(node) ||
    ts.isNumericLiteral(node) ||
    ts.isNoSubstitutionTemplateLiteral(node) ||
    (ts.isTemplateExpression(node) && node.templateSpans.length === 0) ||
    node.kind === ts.SyntaxKind.TrueKeyword ||
    node.kind === ts.SyntaxKind.FalseKeyword ||
    node.kind === ts.SyntaxKind.BigIntLiteral ||
    node.kind === ts.SyntaxKind.NullKeyword ||
    node.kind === ts.SyntaxKind.UndefinedKeyword ||
    ts.isObjectLiteralExpression(node) ||
    ts.isArrayLiteralExpression(node)
  );
}

export function areLiteralsAndSameType(node: ts.BinaryExpression): boolean {
  const isLiteral = (n: ts.Node): boolean =>
    ts.isLiteralExpression(n) ||
    n.kind === ts.SyntaxKind.TrueKeyword ||
    n.kind === ts.SyntaxKind.FalseKeyword;

  return isLiteral(node.left) &&
    isLiteral(node.right) &&
    typeof getLiteralValue(node.left) === typeof getLiteralValue(node.right);
}

export function getLiteralValue(node: ts.Node): string | number | boolean | null {
  if (ts.isStringLiteral(node)) { return node.text };
  if (ts.isNumericLiteral(node)) { return parseFloat(node.text) };
  if (node.kind === ts.SyntaxKind.TrueKeyword) { return true };
  if (node.kind === ts.SyntaxKind.FalseKeyword) { return false };
  return null;
}

export function isTypeOfBinary(node: ts.BinaryExpression): boolean {
  return ts.isTypeOfExpression(node.left) ||
    ts.isTypeOfExpression(node.right);
}

export function isSpecificId(node: ts.Node, name: string): boolean {
    return ts.isIdentifier(node) && node.text === name;
}

export function isSpecificMemberAccess(node: ts.Node, objectName: string, propertyName: string): boolean {
    return (ts.isPropertyAccessExpression(node) &&
        ts.isIdentifier(node.expression) && node.expression.text === objectName &&
        ts.isIdentifier(node.name) && node.name.text === propertyName) ||
        (ts.isElementAccessExpression(node) &&
            ts.isIdentifier(node.expression) && node.expression.text === objectName &&
            ts.isStringLiteral(node.argumentExpression) && node.argumentExpression.text === propertyName);
}

export function isNaNIdentifier(node: ts.Node): boolean {
    if (ts.isParenthesizedExpression(node)) {
        let express = node.expression;
        if (ts.isBinaryExpression(express)) {
            return isNaNIdentifier(express.right);
        }
        return isNaNIdentifier(express);
    }

    return isSpecificId(node, 'NaN') || isSpecificMemberAccess(node, 'Number', 'NaN');
}

export function containsNaN(node: ts.Node): boolean {
  if (isNaNIdentifier(node)) {
    return true;
  }
  if (ts.isParenthesizedExpression(node)) {
    return containsNaN(node.expression);
  }
  if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.CommaToken) {
    return containsNaN(node.right);
  }
  if (ts.isCommaListExpression(node)) {
    return node.elements.some(element => containsNaN(element));
  }
  return false;
}
